package com.dinnerorders.core.services.impl;

import com.dinnerorders.common.models.User;
import com.dinnerorders.common.services.UserService;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

/**
 * @author Sergey Nalivko e-mail: snalivko93@gmail.com
 */
public class UserServiceImpl extends BaseDAO<User> implements UserService {

    private static Logger logger = Logger.getLogger(UserServiceImpl.class);
    /**
     * @inheriDoc
     */
    @Override
    @Transactional
    public void save(User user) {
        super.save(user);
    }

    /**
     * @inheriDoc
     */
    @Override
    @Transactional
    public User findUserByLdapLogin(String ldapLogin) {
        TypedQuery<User> query = entityManager.createNamedQuery(User.FIND_BY_LOGIN_QUERY, User.class);
        query.setParameter("login", ldapLogin);
        User user = null;
        try {
            user = query.getSingleResult();
        } catch (NoResultException nre) {
            logger.warn("Could not found user with login: " + ldapLogin);
        }
        return user;
    }

    /**
     * @inheriDoc
     */
    @Override
    @Transactional
    public User update(User user) {
        return super.update(user);
    }
}
